﻿using Microsoft.EntityFrameworkCore;
using OpenAiClient.Models;
using OpenAiClient.Services;
using OpenAiClient.Tools;

namespace OpenAiClient.Database
{
    // ReSharper disable once IdentifierTypo
    public class SqliteDbContext : DbContext
    {
        public DbSet<HistoryModel>? HistoryModels { get; set; }

        private readonly string _connectionString;

        // ReSharper disable once IdentifierTypo
        public SqliteDbContext(
            DbContextOptions<SqliteDbContext> options,
            IConfigService configService
        ) : base(options)
        {
            _connectionString = Constants.SQLite.GetConnectionString(configService.AppDataDirectory);
        }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            if (!string.IsNullOrWhiteSpace(_connectionString))
            {
                optionsBuilder.UseSqlite(_connectionString);
            }

            optionsBuilder.UseLazyLoadingProxies();

            base.OnConfiguring(optionsBuilder);
        }

        //Load the entities into EF Core
        public void LoadEntities()
        {
            HistoryModels?.Load();
        }
    }
}
